import { getToken, setToken, removeToken } from "@/utils/auth";
import { login, getUserProfile, getUserDetailById } from "@/api/user";

export default {
  namespaced: true,
  state: {
    token: getToken() || "",
    userInfo: "",
  },
  mutations: {
    updateToken(state, token) {
      state.token = token;
      setToken(token);
    },
    removeToken(state) {
      state.taken = "";
      removeToken();
    },
    updataUserInfo(state, info) {
      state.userInfo = info;
    },
    removeUserInfo(state){
      state.userInfo={}
    }
  },
  
  actions: {
    async userLogin(context, loginForm) {   
      const res = await login(loginForm);
      context.commit("updateToken", res.data);
    },
    async getUserInfoProfile(context) {
      try {
        const res = await getUserProfile();
        console.log('getUserInfoProfile',res);
        const info = await getUserDetailById(res.data.userId);
        console.log('getUserDetailById',info);
        context.commit("updataUserInfo", { ...res.data, ...info.data });
      } catch (error) {
        console.log(error);
      }
    },
    logout(context){
      context.commit('removeToken'),
      context.commit('removeUserInfo')
    }
  }
};
